//导出字符的16进制
u_char *ngx_hex_dump(u_char *dst, u_char *src, size_t len)
{
    static u_char  hex[] = "0123456789abcdef";

    while (len--) {
        *dst++ = hex[*src >> 4];
        *dst++ = hex[*src++ & 0xf];
    }

    return dst;
}

//双向链表头插入, sentinel(h)不变
#define ngx_queue_insert_head(h, x) \
    (x)->next = (h)->next; 			\
    (x)->next->prev = x; 			\
    (x)->prev = h; 					\
    (h)->next = x

//双向链表尾插入,sentinel(h)不变
#define ngx_queue_insert_tail(h, x) \
    (x)->prev = (h)->prev; 			\
    (x)->prev->next = x; 			\
    (x)->next = h; 					\
    (h)->prev = x

//wstring 转换 string
std::string ws2s(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest,0,_Dsize);
    wcstombs(_Dest,_Source,_Dsize);
    std::string result = _Dest;
    delete []_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

//string 转换 wstring
std::wstring s2ws(const std::string& s)
{
    setlocale(LC_ALL, "chs"); 
    const char* _Source = s.c_str();
    size_t _Dsize = s.size() + 1;
    wchar_t *_Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest,_Source,_Dsize);
    std::wstring result = _Dest;
    delete []_Dest;
    setlocale(LC_ALL, "C");
    return result;
}

//判断x是不是2的幂
#define IS_POWER_OF_2(x)	(!((x) & ((x) - 1)))

//2的幂且大于size的最小数
unsigned int fixsize(unsigned int size)
{
	size |= size >> 1;
	size |= size >> 2;
	size |= size >> 4;
	size |= size >> 8;
	size |= size >> 16;
	
	return size + 1;
}